# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=c3c
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.7.1
pkgrel=1
pkgdesc="Compiler for the C3 language (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://c3-lang.org'
msys2_repository_url='https://github.com/c3lang/c3c'
license=('spdx:LGPL-3.0-or-later OR MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-lld"
             'git')
source=("git+${msys2_repository_url}.git#tag=v${pkgver}"
        "install-man.patch")
sha256sums=('cc302eca4969d54a53bfd3974f677c18ed53790112ecec1cab2c123d3898a8fc'
            'c800461ae8d1c6be4bb76018a7aee93b5d980664959195ba85c1195686fa5f4d')

prepare() {
  git -C c3c apply ../install-man.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_C_COMPILER="clang" \
      -DCMAKE_CXX_COMPILER="clang++" \
      "${extra_config[@]}" \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target test
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${_realname}"/LICENSE{,_STDLIB} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
